//
//  ViewController.m
//  UIHomeWork19_汽车之家找车页
//
//  Created by tusm on 16/8/9.
//  Copyright © 2016年 tusm. All rights reserved.
//

#import "ViewController.h"
#import "AllBrandlist.h"
#import "HotBrandList.h"
#import "ExtensionList.h"
#import "HotBrandTableViewCell.h"
#import "ExtensionTableViewCell.h"
#import "AllTableViewCell.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *AlltableView;
@property (nonatomic,strong)NSMutableArray <AllBrandlist *> *allBrandListArr;
@property (nonatomic,strong)NSMutableArray <HotBrandList *>*hotBrandListArr;
@property (nonatomic,strong)NSMutableArray <ExtensionList *>*extensionListArr;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.edgesForExtendedLayout = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self handleData];
    [self createTableView];
}
- (void)handleData{
    
    [self handleAllJSON];
    [self handleHotBrandJSON];
    [self handleExtensionJSON];
}
- (void)handleAllJSON{
    NSString *AllJson = [[NSBundle mainBundle] pathForResource:@"All" ofType:@"json"];
    NSData *JSONData = [NSData dataWithContentsOfFile:AllJson];
    NSError *error = nil;
    NSDictionary *Alldic = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingMutableContainers error:&error];
        NSArray *brandlistArray = Alldic[@"result"][@"brandlist"];
    self.allBrandListArr = [NSMutableArray array];
    for (NSDictionary *dic in brandlistArray) {
        AllBrandlist *allBrandList = [[AllBrandlist alloc] initWithDictionary:dic];
        [_allBrandListArr addObject:allBrandList];
//                NSLog(@"++++++++%@",dic);
    }
}
- (void)handleHotBrandJSON{
    NSString *hotBrandJSON = [[NSBundle mainBundle] pathForResource:@"HotBrand" ofType:@"json"];
    NSData *JSONData = [NSData dataWithContentsOfFile:hotBrandJSON];
    NSError *error = nil;
    NSDictionary *hotBrandDic = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingMutableContainers error:&error];
    NSArray *listArray = hotBrandDic[@"result"][@"list"];
    self.hotBrandListArr = [NSMutableArray array];
    for (NSDictionary *dic in listArray) {
        HotBrandList *hotBrandList = [[HotBrandList alloc] initWithDictionary:dic];
        [_hotBrandListArr addObject:hotBrandList];
    }
    
}
- (void)handleExtensionJSON{
    NSString *extensionJSON = [[NSBundle mainBundle] pathForResource:@"Extension" ofType:@"json"];
    NSData *JSONData = [NSData dataWithContentsOfFile:extensionJSON];
    NSError *error = nil;
    NSDictionary *extensionDic = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingMutableContainers error:&error];
    NSArray *listArray = extensionDic[@"result"][@"list"];
    self.extensionListArr = [NSMutableArray array];
    for (NSDictionary *dic in listArray) {
        ExtensionList *extensionList = [[ExtensionList alloc] initWithDictionary:dic];
        [_extensionListArr addObject:extensionList];
    }
    
}





- (void)createTableView{

    [_AlltableView registerNib:[UINib nibWithNibName:@"AllTableViewCell" bundle:nil] forCellReuseIdentifier:@"poolAll"];
    
    [_AlltableView registerClass:[HotBrandTableViewCell class] forCellReuseIdentifier:@"poolHot"];

    [_AlltableView registerClass:[ExtensionTableViewCell class] forCellReuseIdentifier:@"poolExtension"];
}

//返回分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
        return  _allBrandListArr.count + 2;

}
//返回cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 200;
    }else if(indexPath.section == 1){
        return 150;
    }else{
        return 50;
    }
}
//返回分区的标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
        NSString *title = @"热门品牌";
        return title;
    }else if (section == 1){
    NSString *title = @"主打车";
        return title;
    }
    else{
        return _allBrandListArr[section - 2].letter;
    }

}
//返回每个section中的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0 || section == 1) {
        return 1;
    }
    else{
        AllBrandlist *allbrandList = _allBrandListArr[section - 2];
        return allbrandList.list.count;
    }

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        HotBrandTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"poolHot"];
        cell.hotBrandArray = _hotBrandListArr;
        return cell;
    }else if (indexPath.section == 1){
        ExtensionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"poolExtension"];
        cell.extensionArray = _extensionListArr;
        return cell;
    }
    
    else{
        AllTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"poolAll"];
        AllBrandlist *allBrandList = _allBrandListArr[indexPath.section - 2];
        AllList *allList = allBrandList.list[indexPath.row];
        cell.allList = allList;
        return cell;
    }
}

//索引
- (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView {

    NSMutableArray *indexesArr = [NSMutableArray array];
    [indexesArr addObject:@"热"];
    [indexesArr addObject:@"主"];
    for (AllBrandlist *allBrandList in _allBrandListArr) {
        [indexesArr addObject:allBrandList.letter];
    }
    return indexesArr;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
